package com.alibaba.citrus.cr.unified.inventory.contants;

import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crunifiedinventory.model.dto.AllocateOrderStandardMessageDTO;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * @author yifei
 * @since 2024/3/7
 */
public class MessageHelperUtil {
    private static final Log log = Log.getLogger(MessageHelperUtil.class);

    private final static String BUSINESS_TYPE = "99";

    private final static String BUSINESS_UNIT_CODE = "XX";

    /**
     * 校验数据的有效性
     * @param dto
     * @return
     */
    public static boolean checkAllocateOrderStandardMessageDTO(AllocateOrderStandardMessageDTO dto){
        log.info("MessageHelperUtil_checkAllocateOrder_dto:{}", JSON.toJSONString(dto));
        if (dto == null){
            log.error("MessageHelperUtil_checkAllocateOrder_dto_is_null");
            return false;
        }
        if (StringUtils.isBlank(dto.getBusinessType())) {
            log.info("MessageHelperUtil_checkAllocateOrder_dto_businessType_is_empty");
            return false;
        }
        if (StringUtils.isBlank(dto.getFromSys())){
            log.info("MessageHelperUtil_checkAllocateOrder_dto_fromSys_is_empty");
            return false;
        }
        if (StringUtils.isBlank(dto.getKeyword())){
            log.info("MessageHelperUtil_checkAllocateOrder_dto_keyword_is_empty");
            return false;
        }
        if (!MessageEventCodeEnum.isExist(dto.getEventCode())){
            log.info("MessageHelperUtil_checkAllocateOrder_dto_eventCode_is_not_exist");
            return false;
        }
        if (!MessageBusinessCodeEnum.isExist(dto.getBusinessCode())){
            log.info("MessageHelperUtil_checkAllocateOrder_dto_businessCode_is_not_exist");
            return false;
        }
        // 填充名称
        dto.setEventName(MessageEventCodeEnum.getNameByCode(dto.getEventCode()));
        dto.setBusinessName(MessageBusinessCodeEnum.getNameByCode(dto.getBusinessCode()));
        return true;
    }

    public static String buildMessageKey(AllocateOrderStandardMessageDTO dto) {
        StringBuilder builder = new StringBuilder();
        builder.append(dto.getFromSys());
        builder.append("-");
        builder.append(dto.getBusinessType());
        builder.append("-")
                .append(BUSINESS_UNIT_CODE)
                .append("-")
                .append(dto.getEventCode())
                .append("-")
                .append(dto.getBusinessCode())
                .append("-")
                .append(StringUtils.replace(UUID.randomUUID().toString(), "-",""));
        return builder.toString();
    }
}
